//
//  RegisterViewController.m
//  projectOfTeamSample
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 liukai. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import "UserDetaileTableViewController.h"
#import "loginViewController.h"
#import <AVUser.h>
@interface RegisterViewController ()
@property (nonatomic,strong)RegisterView *registV;
@end

@implementation RegisterViewController

-(void)loadView{
    [super loadView];
    
    self.registV = [[RegisterView alloc]initWithFrame:self.view.frame];
    self.registV.backgroundColor = [UIColor whiteColor];
     self.view = self.registV;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.registV.certainButton addTarget:self action:@selector(didClickCertainButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.registV.cancaleButton addTarget:self action:@selector(didClickCancleButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.registV.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"reg"]];
    [self.registV.verifyButton addTarget:self action:@selector(didClickVerifyButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
   
}
//注册
-(void)didClickCertainButtonAction:send{
    [AVUser verifyMobilePhone:self.registV.verifyText.text withBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"友情提醒,邮箱验证码已经发送,验证邮箱,找密码不费事哦" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"提醒了哦" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
              [self.navigationController popViewControllerAnimated:YES];
            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            NSString *errorString = [NSString stringWithFormat:@"%@",error];
            NSRange range = NSMakeRange(45, 34);
            NSMutableString *string = (NSMutableString *)[errorString substringWithRange:range];
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"注册失败" message:string preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *re_registrationAction = [UIAlertAction actionWithTitle:@"重新注册" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            }];
            
            UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                [self.navigationController pushViewController:[[UserDetaileTableViewController alloc]init] animated:YES];
            }];
            
            [alertC addAction:re_registrationAction];
            [alertC addAction:cancleAction];
            
            [self presentViewController:alertC animated:YES completion:nil];
        }
    }];
}
//取消
-(void)didClickCancleButtonAction:send{
    [self.navigationController popViewControllerAnimated:YES];
}
//获取验证码
-(void)didClickVerifyButtonAction:send{
    AVUser *user = [AVUser user];
    user.username = self.registV.userText.text;
    user.password = self.registV.passwordText.text;
    user.email = self.registV.maileNumber.text;
    [user setObject:self.registV.phoneNumber.text forKey:@"mobilePhoneNumber"];
    
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
           
            
        }else{
            NSString *errorString = [NSString stringWithFormat:@"%@",error];
            NSRange range = NSMakeRange(45, 34);
            NSMutableString *string = (NSMutableString *)[errorString substringWithRange:range];
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"获取失败" message:string preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *re_registrationAction = [UIAlertAction actionWithTitle:@"重新获取" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            }];
            
            UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                [self.navigationController pushViewController:[[UserDetaileTableViewController alloc]init] animated:YES];
            }];
            
            [alertC addAction:re_registrationAction];
            [alertC addAction:cancleAction];
            
            [self presentViewController:alertC animated:YES completion:nil];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
